package com.remon.quartz.exception;

import github.remonlong.restfulutil.exception.ExceptionHandle;
import github.remonlong.restfulutil.exception.RestfulResultUtil;
import github.remonlong.restfulutil.exception.Result;
import github.remonlong.restfulutil.exception.api.BadApiException;
import github.remonlong.restfulutil.exception.enumeration.EnumResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <h2>全局异常处理类</h2>
 *
 * @author remon
 * @create 2018-08-15 17:02
 **/
@ControllerAdvice
@ResponseBody
public class ExceptionRemonHanlder {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionRemonHanlder.class);

    /**
     * 接口异常
     *
     * @param e @Link { BadApiException }
     * @return
     */
    @ExceptionHandler(BadApiException.class)
    public Result<String> exception(BadApiException e) {
        ExceptionHandle exceptionHandle = new ExceptionHandle();
        return exceptionHandle.bindRestfulException(e);
    }

    /**
     * 未知异常处理
     *
     * @param e @Link { Exception }
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result<String> exception(Exception e) {
        logger.error("发生未知异常 {}", e);
        return RestfulResultUtil.error(EnumResult.SYSTEM_ERROR);
    }

    /**
     * 参数格式校验失败
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<String> exception(HttpMessageNotReadableException e) {
        return RestfulResultUtil.error(EnumResult.PARAM_VALID_ERROR);
    }
}
